﻿using MTEA.LIB.Analysis.KDJ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTEA.MTTask
{
    public class KDJMTTask : MTTask<KDJMTTask.KDJMTTaskConfig>
    {
        public static readonly string Tag = "KDJMTTask";
        public class KDJMTTaskConfig : MTTaskConfig
        {
            public int Shift { get; set; }
            /// <summary>
            /// 0 是到达指定区域,1 在某区域交叉
            /// </summary>
            public int KDJType { get; set; }

            public KDJCrossType CrossType { get; set; }
            public double MaxValue { get; set; }

            public double MinValue { get; set; }

        }

        public KDJMTTask(KDJMTTaskConfig config) : base(config)
        {
            Name = "KDJ Task";
            Description = MTTaskConfig.GetDescription(config);
        }

        public override void Execute(MTTaskContext context)
        {
            base.Execute(context);
            if(config.KDJType==0)
            {
                var main = client.getKDJMain(context.Symbol, config.TimeFrames, config.Shift);
                if(main>=config.MinValue && main <=config.MaxValue)
                {
                    isFinished=OrderTrade(Tag, context);
                    return;
                }
            }
            else if(config.KDJType==1)
            {
                var result = client.getKDJCross(context.Symbol, config.TimeFrames, config.Shift, config.Shift + 1);
                if(result.CrossTye == config.CrossType && result.Value >= config.MinValue && result.Value <= config.MaxValue)
                {
                    isFinished = OrderTrade(Tag, context);
                    return;
                }
            }
            else
            {
                throw new ArgumentException("Invalid KDJType");
            }
            isFinished = false;
        }

    }
}
